#include "AdminInfoMag.h"
#include "ui_AdminInfoMag.h"

AdminInfoMag::AdminInfoMag(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AdminInfoMag)
{
    ui->setupUi(this);
    setWinStyle();
    adminTableOP();
    connect(ui->usernameLineEdit, SIGNAL(returnPressed()), this, SLOT(on_searchPushButton_clicked()));
}

AdminInfoMag::~AdminInfoMag()
{
    delete ui;
}

void AdminInfoMag::adminTableOP()
{
    M = new QSqlTableModel;
    M->setTable("admin");
    M->select();
    ui->tableView->setModel(M);
    ui->tableView->setColumnHidden(0, true);
    QString headStr[] = {"ID", "用户名", "密码", "管理员类型"};

    for(int i=0; i<4; i++)
    {
        M->setHeaderData(i, Qt::Horizontal, headStr[i]); // 设置表格 column 列名称
    }
    //ui->tableView->verticalHeader()->setVisible(false); // 隐藏 第一列 名称
    ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView::section {"
                                                     "color: brown;padding-left: 1px;border: 1px solid #6c6c6c;}");
    // 设置表格 column 列的样式
    ui->tableView->horizontalHeader()->setMinimumHeight(30); // 设置表头高度
}


void AdminInfoMag::setWinStyle()
{
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->usernameLineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                        "color: rgb(31,31,31);"		//字体颜色
                                        "padding-left:20px;"       //内边距-字体缩进
                                        "background-color: rgb(255, 255, 255);" //背景颜色
                                        "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->searchPushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");

    ui->addPushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");


    ui->deletePushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(251,102,102, 200);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(20,196,188, 210);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");


}

void AdminInfoMag::on_searchPushButton_clicked()
{
    QString username = ui->usernameLineEdit->text();
    QString adminType = ui->adminTypeComboBox->currentText();
    QString sql;
    if (adminType == "全部") {
        adminType = "";
    }
    if (username.isEmpty() && adminType.isEmpty()) {
        sql = "";
    }
    else if (username.isEmpty()) {
        sql = QString("adminType like '%%1%'").arg(adminType);
    }
    else if (adminType.isEmpty()) {
        sql = QString("username like '%%1%'").arg(username);
    }
    else {
        sql = QString("(username like '%%1%') AND (adminType like '%%2%')").arg(username, adminType);
    }
    M->setFilter(sql);
}


void AdminInfoMag::on_deletePushButton_clicked()
{
    QMessageBox msgBox;
    msgBox.setWindowTitle("管理员管理");
    msgBox.setText("提示");
    msgBox.setInformativeText("确实要删除吗?");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Ok);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Ok){
        QModelIndex curIndex = ui->tableView->currentIndex();
        M->removeRow(curIndex.row());
        M->select();
    }
}


void AdminInfoMag::on_addPushButton_clicked()
{
    admin_add = new administrator_info_add(M, this);
    admin_add->show();
    M->select();
}

